package cc.facesign.admin.service.impl;

import cc.facesign.admin.mapper.AclMapper;
import cc.facesign.admin.service.AclService;
import cc.facesign.admin.service.TblUserService;
import cc.facesign.admin.utils.MqUtils;
import cc.facesign.common.exception.FacesignException;
import cc.facesign.common.type.MqKey;
import cc.facesign.common.type.RoleType;
import cc.facesign.common.utils.bean.R;
import cc.facesign.security.bean.LoginUser;
import cc.facesign.servicebase.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class AclServiceImpl implements AclService {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private TblUserService userService;

    @Autowired
    private MqUtils mqUtils;

    @Resource
    private AclMapper baseMapper;

    @Override
    public Set<String> getPerms(String userId) {
        LoginUser loginUser =  redisCache.getCacheObject("login:"+userId);
        if (loginUser == null) {
            throw new FacesignException(R.ERROR, "登录状态异常");
        }
        List<String> permissions = loginUser.getPermissions();
        return new HashSet<>(permissions);
    }

    @Override
    public String getSecret(String password) {
        return passwordEncoder.encode(password);
    }

    @Override
    public boolean match(String password, String secret) {
        return passwordEncoder.matches(password, secret);
    }

    @Override
    public void initRole(String deptId, String userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("key", MqKey.MAP_INTI_ROLE);
        map.put("userId", userId);
        map.put("rid", deptId);

        mqUtils.sendMap(map);
    }

    @Override
    public void setManage(String userId, String id, String type) {
        Map<String, Object> map = new HashMap<>();
        map.put("key", MqKey.MAP_SET_MANAGE);
        map.put("rid", id);
        map.put("type", type);
        map.put("userId", userId);

        mqUtils.sendMap(map);
    }

    @Override
    public void deleteManage(String userId, String rid, String type) {
        Map<String, Object> map = new HashMap<>();
        map.put("key", MqKey.MAP_DELETE_MANAGE);
        map.put("rid", rid);
        map.put("userId", userId);
        map.put("type", type);

        // 判断是否可以取消role权限（如果该用户还管理者其他会议或部门的话）
        if (canDelete(userId, type) ) {
            mqUtils.sendMap(map);
        }
    }

    private boolean canDelete(String userId, String type) {

        if (type.equals("conf")) {
            return userService.countManageForConf(userId) == 0;
        }

        if (type.equals("dept")) {
            return userService.countManageForDept(userId) == 0;
        }

        if (type.equals(RoleType.ROOT)) {
            return true;
        }

        return true;

    }


}
